-- 此项目仅供呼吸测试
local info = {
    {"3组投石机所需材料【打蜂后】", 
    [[
        6木板 6电子元件
        54树枝 270石头 63草 21蜘蛛丝
    ]]},
    -- {字符串1, 字符串2}

    {"奶奶岛修建材料",[[
        1、10切割机碎片 10木板 1萤火虫
        2、10大理石 5石砖 3荧光果
        3、10月岩 5绳子 5牛毛地皮
    ]]},
    {
        "种地搭配",[[
            季节    1:1             1:2                 1:1:1                   1:2:2
            秋      土豆:番茄       辣椒 番茄           大蒜 洋葱 辣椒              洋葱 土豆 玉米
            冬                                          南瓜/萝卜 土豆 芦笋         大蒜 土豆 南瓜/萝卜
            春      土豆:番茄       火龙果 番茄         大蒜 洋葱 火龙果            洋葱 土豆 玉米/芦笋
            夏                      辣椒/火龙果 番茄    大蒜 洋葱 辣椒/火龙果	            
        ]]
    },
    {
        "种地材料",[[
            4耕地机 12木板 8绳子 8碎石
            1水壶 2木板 1绳子
            1鸟笼 8芦苇 2种子 6金子
            1魔法本 4兔子 4木板 6蜘蛛丝
            1乐器   4噩梦燃料 2猪皮 4金子
            1遮阳棚 2蜘蛛丝 4木板 3绳子
            1钓竿 2树枝 2蜘蛛丝
            1矿工帽 1草帽 1金子 4树枝 2蜘蛛丝 1绳子
            1铲子 4树枝 2金子

            共计 22木板 13绳子 8碎石 8芦苇 13金子
                    4兔子 12蜘蛛丝 4噩梦燃料 2猪皮
                    10树枝 1草帽
            堆肥桶 3木板+1腐烂物+1草
        ]]
    }
}

function GLOBAL.showme(i)
    print("***************************")
    if info[i] and info[i][1] and info[i][2] then
        print("##", info[i][1], "##\n")
        print(info[i][2])
    else
        print("目录:")
        for k,v in pairs(info)do
            if v and type(v[1]) == "string" then
                print(k.."、"..v[1])
            end
        end
    end
end

function GLOBAL.LATELY(time, func)
    print("一个任务将会在",time,"秒后执行")
    GLOBAL.CreateEntity():DoTaskInTime(time, func)
end
function GLOBAL.showfunc()
    print("nearEnt(name, range, allowTags, banTags, allowAnims, banAnims)")
    print("nearSlot(id)")
    print("printDist(a_ent, b_ent)")
    print("nearEnts(range, allowTags, banTags)")
    print("getTags(ent)")
    print("compTags(tags1, tags2)")
    print("getItems(prefab, tags)")
    print("getItem(prefab, tag)")
    print("getSlot(slot)")
    print("fepAnim(ent, cd, stop)")
    print("LATELY(time, func)")
    print("loopStart(cd, func)")
    print("loopStop()")
    print("watchRPC()")
    print("gHUD() -> ghud()")
end

function GLOBAL.fepAnim(ent, cd, stop)
    cd = cd or 0.3
    stop = stop or 30
    local anims = {}
    GLOBAL.ThePlayer.COMMANDANIMTASK = ent:DoPeriodicTask(cd, function(inst)
        local anim = GetAnim(inst)
        if table.contains(anims, anim) then
        else
            print(anim)
            table.insert(anims, anim)
        end
    end)
    GLOBAL.ThePlayer:DoTaskInTime(stop, function(inst)
        if inst.COMMANDANIMTASK then
            inst.COMMANDANIMTASK:Cancel()
            inst.COMMANDANIMTASK = nil
            print("追踪",ent and ent.name,"的动画已停止","CD:",cd,"STOP:",stop)
            print("***************************")
        end
    end)
    print("***************************")
    print("追踪",ent and ent.name,"的动画已开始","CD:",cd,"STOP:",stop)
    return GLOBAL.ThePlayer.COMMANDANIMTASK
end

GLOBAL.nearEnt = PlayerFindEnt
GLOBAL.nearEnts = PlayerFindEnts

function GLOBAL.nearSlot(loc)
    return GLOBAL.ThePlayer.replica.inventory:GetItemInSlot(loc)
end

function GLOBAL.printDist(ent1, ent2)
    if ent1 and ent1:IsValid() and ent2 and ent2:IsValid() then
        local dis = math.sqrt(ent1:GetPosition():DistSq(ent2:GetPosition()))
        print(dis)
        return dis
    end
end

function GLOBAL.getTags(ent)
    local tags_string = ent:GetDebugString():match("Tags:(.-)\n")
    return tags_string and tags_string:split(" ") or {}
end

function GLOBAL.compTags(tags1, tags2)
    local dif1 = {}
    local dif2 = {}
    for _,tag in pairs(tags1)do
        if not table.contains(tags2, tag) then
            table.insert(dif1, #dif1+1, tag)
        end
    end
    print("在 TAG1 而不在 TAG2 中:")
    print(unpack(dif1))
    for _,tag in pairs(tags2)do
        if not table.contains(tags1, tag) then
            table.insert(dif2, #dif2+1, tag)
        end
    end
    print("在 TAG2 而不在 TAG1 中:")
    print(unpack(dif2))
end

function GLOBAL.getItems(...)
    return GetItemsFromAll(...)
end

function GLOBAL.getItem(...)
    return GetItemFromAll(...)
end

function GLOBAL.getSlot(...)
    return GetEquippedItemFrom(...)
end

local looptask = false
function GLOBAL.loopStop()
    if looptask then
        looptask:Cancel()
    end
    looptask = false
end
function GLOBAL.loopStart(cd, func)
    if looptask then
        looptask:Cancel()
    end
    looptask = GLOBAL.TheGlobalInstance:DoPeriodicTask(cd, func())
    return looptask
end

--- 调试用
local testid = 0
function fep(Ttable)
    if type(Ttable) ~= "table" then
        print("这不是表, 数据类型：",type(Ttable), "直接打印：",Ttable)
    else      
        print("********************欢迎帮助FEP开发***************************")    
        for k, v in pairs(Ttable) do
            print(k, v)
        end
        print("********************FEP", testid ,"结束*********************")
        testid = testid + 1
    end
end
function GLOBAL.FEP(Ttable)
    fep(Ttable)
end
function GLOBAL.FEP_M(t)
    if type(t) ~= "table" then
        print("这不是表, 数据类型：",type(t), "直接打印：",t)
        return
    end
    local mt = {}
    for k, v in pairs(getmetatable(t)) do
        if table.contains({"is_instance", "is_class", "is_a", "__index", "_ctor"}, k) then
        else
            mt[k] = v
        end
    end
    fep(mt)
    return mt
end
function GLOBAL.FEP_MM(t)
    local mt = getmetatable(t)
    fep(mt)
    return mt
end
function GLOBAL.FEP_K(t, valuetype)
    if type(t) ~= "table" then
        print("这不是表, 数据类型：",type(t), "直接打印：",t)
        return
    end
    local t_key = {}
    local str = ""
    local lines = {}
    for k,v in pairs(t)do
        if (type(v) == valuetype) or (not valuetype) then
            local strk = tostring(k)
            table.insert(t_key, strk)
            if (#str + #strk) <  100 then
                str = str..strk..", "
            else
                table.insert(lines, str)
                str = strk..", "
            end
        end
    end
    table.insert(lines, str)
    print("********************FEP", testid ,"内容*********************")
    print(table.concat(lines, "\n"))
    if valuetype then
        print("*********",t,"长度",#t_key,"指定类型：", valuetype, "*********")
    else
        print("*********",t,"长度",#t_key, "*********")
    end
    testid = testid + 1
    return t_key
end
function GLOBAL.FEP_V(t)
    if type(t) ~= "table" then
        print("这不是表, 数据类型：",type(t), "直接打印：",t)
        return
    end
    local t_key = {}
    local str = ""
    local lines = {}
    for _,k in pairs(t)do
        local strk = tostring(k)
        table.insert(t_key, strk)
        if (#str + #strk) <  100 then
            str = str..strk..", "
        else
            table.insert(lines, str)
            str = strk..", "
        end
    end
    table.insert(lines, str)
    print("*********FEP", testid ,"内容*********")
    print(table.concat(lines, "\n"))
    print("********************",t,"长度",#t_key,"*********************")
    testid = testid + 1
    return t_key
end


function GLOBAL.Pfunc(func)
    if type(func) == "function" then
        local str = string.format("loadstring(%q)", string.dump(func))
        print(str)
    else
        print(""..func.." 不是一个函数！")
    end
end

function GLOBAL.Puser(value)
    if type(value) == "userdata" then
        return print(""..value.."不是userdata！")
    end
    local var
    local i = 1
    while true do
        local n, v = debug.getlocal(3, i)
        print(n,v,var)
        if not n then break end
        if n=="subpath" then
            var = v
        end
        --print(n,v)
        i = i + 1
    end
    GLOBAL.error("Cannot dump userdata ("..GLOBAL.tostring(value).." - "..(var or "unknown")..")")
end

local _SendRPCToServer = GLOBAL.SendRPCToServer
GLOBAL.MOD_ShroomCake.watch_rpc_state = false
function GLOBAL.watchRPC()
    GLOBAL.MOD_ShroomCake.watch_rpc_state = not GLOBAL.MOD_ShroomCake.watch_rpc_state
    if GLOBAL.MOD_ShroomCake.watch_rpc_state then
        print("*********************开始记录*****************************")
        GLOBAL.SendRPCToServer = function(rpc, ac, sth, ...)
            local ri, ai 
            for k,v in pairs(GLOBAL.RPC)do 
                if rpc == v then 
                    ri = k 
                    break
                end 
            end 
            if not ri then ri = rpc end 
            for k,v in pairs(GLOBAL.ACTIONS)do 
                if v.code == ac then 
                    ai = k 
                    break
                end 
            end 
            if not ai then ai = ac end 
            print(ri, ai, "3:",sth, "4：", ...) 
            _SendRPCToServer(rpc, ac, sth, ...) 
        end
    else
        print("*********************记录停止*****************************")
        GLOBAL.SendRPCToServer = _SendRPCToServer
    end
end


GLOBAL.p_util = require "libs/playerutil"
GLOBAL.e_util = require "libs/entutil"
GLOBAL.t_util = require "libs/tableutil"
GLOBAL.s_util = require "libs/settingmanager"
GLOBAL.c_util = require "libs/calcutil"
GLOBAL.h_util = require "libs/hudutil"
GLOBAL.i_util = require "libs/inpututil"

GLOBAL.w_mana, GLOBAL.w_util = {}, {}
GLOBAL.pc, GLOBAL.picker = {}, {}
AddPlayerActivatedFunc(function(player, world)
    GLOBAL.w_mana = require "libs/worldmanager" 
    GLOBAL.w_util = require "libs/worldutil"  
    GLOBAL.pc = player.components.playercontroller
    GLOBAL.picker = player.components.playeractionpicker
end)


GLOBAL.ghud = {}
GLOBAL.gHUD = function()
    GLOBAL.TheGlobalInstance:DoTaskInTime(1, function()
        GLOBAL.ghud = GLOBAL.TheInput:GetHUDEntityUnderMouse()
        print("ghud:", GLOBAL.ghud)
    end)
end

    -- a = TheFrontEnd:GetActiveScreen()
-- scroll_area.context.widget.children


-- pc = ThePlayer.components.playercontroller
-- picker = ThePlayer.components.playeractionpicker
-- TheFocalPoint.SoundEmitter:PlaySound("dontstarve/HUD/click_negative")